//
//  PHNewFeatureViewController.swift
//  PHWeibo
//
//  Created by 潘荟 on 16/7/22.
//  Copyright © 2016年 BAT. All rights reserved.
//

import UIKit

class PHNewFeatureViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    // MARK: - 设置界面
    func setupUI() {
        //添加子控件
        view.addSubview(collectionView)
        
        //设置约束
        collectionView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        
    }
    
    // MARK: - 懒加载子控件
    private lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        //设置layout属性
        layout.itemSize = CGSize(width: SCREENW, height: SCREENH)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        

        
        let collectionV = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        collectionV.registerClass(PHNewFeatureViewCell.self, forCellWithReuseIdentifier: "cell")
        
        collectionV.delegate = self
        collectionV.dataSource = self
        collectionV.pagingEnabled = true
        collectionV.bounces = false
        collectionV.showsHorizontalScrollIndicator = false
        
        
        return collectionV
        
    }()
    
    
    
    
    

}
// MARK: - 代理方法
extension PHNewFeatureViewController:UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! PHNewFeatureViewCell
        
        cell.imageIndex = indexPath.item
        
        return cell
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / SCREENW)
        
        if index == 3 {
            let cell = self.collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! PHNewFeatureViewCell
            
            cell.startAnim()
        }
    }
}

// MARK: - 自定义cell
class PHNewFeatureViewCell: UICollectionViewCell {
    
    //设置一个属性用来给cell添加图片
    var imageIndex:Int = 0 {
        didSet{
            backImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置动画
    func startAnim() {
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    // MARK: - 设置界面
    func setupUI() {
        //添加子控件
        contentView.addSubview(backImageView)
        

        
        contentView.addSubview(startButton)
        
        

    
        //约束子控件
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
    }
    
    
    // MARK: - 实现按钮的监听方法
    func buttonClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNoti, object: "newFeature")
    }
    
    
    // MARK: - 懒加载子控件
    private lazy var backImageView:UIImageView = UIImageView()
    
    private lazy var startButton:UIButton = {
       let startBtn = UIButton()
        
        startBtn.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        startBtn.setTitle("欢迎你妈隔壁", forState: UIControlState.Normal)
        startBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        startBtn.titleLabel?.font = UIFont.systemFontOfSize(18)
        startBtn.sizeToFit()
        
        startBtn.hidden = true
        
        return startBtn
    }()
    
    
    
}













